<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="supplierNo">
        <el-input
          v-model="queryParams.supplierNo"
          placeholder="请输入编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="开户行" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入开户行"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="银行账户" prop="bankAccount">
        <el-input
          v-model="queryParams.bankAccount"
          placeholder="请输入银行账户"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应付款" prop="payableAmount">
        <el-input
          v-model="queryParams.payableAmount"
          placeholder="请输入应付款"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobileNo">
        <el-input
          v-model="queryParams.mobileNo"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="座机号" prop="telNo">
        <el-input
          v-model="queryParams.telNo"
          placeholder="请输入座机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contact">
        <el-input
          v-model="queryParams.contact"
          placeholder="请输入联系人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="级别" prop="level">
        <el-input
          v-model="queryParams.level"
          placeholder="请输入级别"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

   

    <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <el-table-column label="供应商编号" align="center" prop="supplierNo" />
      <el-table-column label="名称" align="center" prop="supplierName" />
      <!-- <el-table-column label="开户行" align="center" prop="bankName" />
      <el-table-column label="银行账户" align="center" prop="bankAccount" /> -->
      <el-table-column label="应付款" align="center" prop="payableAmount" width="260" :show-overflow-tooltip="true">
        <template #default="scope">
          <p>{{ scope.row.payableAmount }}</p>
          <el-button link type="primary" @click="dialogFormVisijia(scope.row.supplierNo)">+应收</el-button>
          <el-button link type="primary" @click="dialogFormVijie(scope.row.supplierNo)">-结款</el-button>
            <router-link :to="'/wms/Transaction/index/' + scope.row.supplierNo" class="link-type">
                  查看流水
            </router-link  >
        </template>
      </el-table-column>
      <!-- <el-table-column label="地址" align="center" prop="address" /> -->
      <!-- <el-table-column label="手机号" align="center" prop="mobileNo" /> -->
      <!-- <el-table-column label="座机号" align="center" prop="telNo" /> -->
      <el-table-column label="联系人" align="center" prop="contact" />
      <el-table-column label="级别" align="center" prop="level" />
      <!-- <el-table-column label="邮箱" align="center" prop="email" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        
      </el-table-column>
    </el-table>
    

    <!-- 新增应付款对话框 -->
   <el-dialog v-model="dialogFormVisible" :title="title1" width="500">
    <el-form :model="form1">
      <el-form-item label="金额" :label-width="formLabelWidth">
        <el-input-number v-model="form1.transactionAmount" @change="handleChange" />
      </el-form-item>
      <el-form-item label="备注" :label-width="formLabelWidth"> 
        <el-input v-model="form1.remark" autocomplete="off" type="textarea" />
      </el-form-item>
    </el-form>
  
    <template #footer>
      <div class="dialog-footer">
          <el-button @click="dialoqx()">取消</el-button>
          <el-button type="primary" @click="submitFormVisible()" v-hasPermi="['wms:SupplierTransaction:add']">
            确定
          </el-button>
      </div>
    </template>
  </el-dialog>


    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="supplierRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="supplierNo">
          <el-input v-model="form.supplierNo" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="名称" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="开户行" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入开户行" />
        </el-form-item>
        <el-form-item label="银行账户" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账户" />
        </el-form-item>
        <el-form-item label="应付款" prop="payableAmount">
          <el-input v-model="form.payableAmount" placeholder="请输入应付款" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="form.mobileNo" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="座机号" prop="telNo">
          <el-input v-model="form.telNo" placeholder="请输入座机号" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="form.level" placeholder="请输入级别" />
        </el-form-item>
        <el-form-item label="Emall" prop="email">
          <el-input v-model="form.email" placeholder="请输入Emall" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <!-- <el-form-item label="删除标识" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标识" />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Supplier">
import { listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier } from "@/api/wms/supplier";
import { addTransaction ,selectSupplierTransactionByCodeID} from '@/api/wms/transaction'

const { proxy } = getCurrentInstance();

const supplierList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const title1 = ref("");
const dialogFormVisible = ref(false);
const num = ref(0);
const formLabelWidth = ref(50)

const data = reactive({
  form1:{
    transactionAmount:null,
  },
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    supplierNo: null,
    supplierName: null,
    bankName: null,
    bankAccount: null,
    payableAmount: null,
    address: null,
    mobileNo: null,
    telNo: null,
    contact: null,
    level: null,
    email: null,
  },
  rules: {
    supplierNo: [
      { required: true, message: "供应商编号不能为空", trigger: "blur" }
    ],
    supplierName: [
      { required: true, message: "供应商名称不能为空", trigger: "blur" }
    ],
    delFlag: [
      { required: true, message: "删除标识不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules, form1 } = toRefs(data);


/** 查询供应商列表 */
function getList() {
  loading.value = true;
  listSupplier(queryParams.value).then(response => {
    supplierList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form1.value={
    transactionAmount:0,
    remark:null,
    supplierId:null,
    transactionType:null
  };
  form.value = {
    id: null,
    supplierNo: null,
    supplierName: null,
    bankName: null,
    bankAccount: null,
    payableAmount: null,
    address: null,
    mobileNo: null,
    telNo: null,
    contact: null,
    level: null,
    email: null,
    remark: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("supplierRef");
}

//获取交易编号
const Code=ref('Ts_'+new Date().getTime())
//定义一个空对象来接受当前余额以及客户id
const List=ref({})



/** 应收结款框的确定按钮事件 */
const submitFormVisible = () => {
  form1.value['transactionCode']=Code.value
  addTransaction(form1.value).then(resp=>{
    selectSupplierTransactionByCodeID(Code.value).then(response=>{
      console.log(response)
      List.value.payableAmount=response.data.currentBalance
      console.log(List.value.receivableAmount)
      List.value.supplierNo=response.data.supplierId
      updateSupplier(List.value).then(response => {
        console.log(response)
        getList()
         location.reload();
      });
    })
  }).catch({});
  console.log(form1.value)
  dialogFormVisible.value=false;
}

/** 新增应付结款的取消按钮 */
const dialoqx = () => {
  dialogFormVisible.value=false;
  num.value=0;
}

/** 金额输入框 */
const handleChange = (value) => {}

/** 应付 */
function dialogFormVisijia(no) {
  reset();
  form1.value={
    supplierId:no,
    transactionType:'1',
  };
  dialogFormVisible.value=true;
  title1.value='新增应付款';
}

/** 结款 */
function dialogFormVijie(no) {
  reset();
  form1.value={
    supplierId:no,
    transactionType:'2',
  };
  dialogFormVisible.value=true;
  title1.value='结款操作';
}


/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加供应商";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getSupplier(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改供应商";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["supplierRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateSupplier(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSupplier(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除供应商编号为"' + _ids + '"的数据项？').then(function() {
    return delSupplier(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/supplier/export', {
    ...queryParams.value
  }, `supplier_${new Date().getTime()}.xlsx`)
}

getList();
</script>
